package com.wang.mandkey.tool;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.util.Log;

import com.wang.mandkey.views.PointFBean;

public class SeariesTool {

	private static String tag = "SeariesTool";
	public static String baseDir = "keyBoad";

	/**
	 * 保存按键方案
	 * 
	 * @param context
	 * @param pflist
	 * @param fileName
	 * @throws IOException
	 */
	public static void savePointFBean(Context context,
			ArrayList<PointFBean> pflist, String fileName) throws IOException {
		File filedir = context.getFilesDir();
		// 01-05 14:39:26.585: INFO/SeariesTool(766):
		// filedir:/data/data/com.wang.mandkey.activity/files

		File fdir = new File(filedir.toString() + File.separator + baseDir);
		if (fdir.isFile() || fdir.exists() == false) {
			fdir.delete();
			fdir.mkdir();
		}

		File tf = new File(fdir.toString() + File.separator + fileName);
		if (tf.isFile() && tf.exists()) {
			tf.delete();
		}
		tf.createNewFile();
		OutputStream outputStream = new FileOutputStream(tf);
		ObjectOutputStream objectout = new ObjectOutputStream(outputStream);
		for (PointFBean pf : pflist) {
			objectout.writeObject(pf);
		}
		outputStream.flush();
		outputStream.close();
		objectout.close();
		Log.i(tag, "filedir:" + filedir.toString());
	}



	/**
	 * 加载pointflist
	 * @param context
	 * @param fname
	 * @return
	 */
	public static ArrayList<PointFBean> loadPointFList(Context context,String fname){
		ArrayList<PointFBean> pfblist=null;

		 File filedir = context.getFilesDir();
		  File keyboardFile=new File(filedir.toString()+File.separator+baseDir+File.separator+fname);
		  if(keyboardFile.isFile() && keyboardFile.exists()){
			   try {
				   ObjectInputStream oin=new ObjectInputStream(new FileInputStream(keyboardFile));
				   PointFBean tempPfb=null;
				   try {
					   pfblist=new ArrayList<PointFBean>();
					 while((tempPfb=(PointFBean) oin.readObject())!=null){
					    	pfblist.add(tempPfb);
					    }
						} catch (EOFException e) {
							//文件结束
							e.printStackTrace();
						}
				      oin.close();
			} catch (Exception e) {
				e.printStackTrace();
			} 
			      
		  }
		
		  return pfblist;
	}
	

	
	/**
	 * 通过文件名删除方案
	 * @param context
	 * @param fname
	 * @return
	 */
	public static void deletePointFList(Context context,String fname){
		 File filedir = context.getFilesDir();
		  File keyboardFile=new File(filedir.toString()+File.separator+baseDir+File.separator+fname);
		  if(keyboardFile.isFile() && keyboardFile.exists()){
			  keyboardFile.delete();
		  }
	}
	
	
	
	
	/**
	 * 所有方案
	 * @param context
	 * @return
	 */
	public static ArrayList<String> loadCaseList(Context context){
		ArrayList<String> arrayListCase=null;
	   File filedir = context.getFilesDir();
	   File keyboarddirs=new File(filedir.toString()+File.separator+baseDir);
	    if(keyboarddirs.isDirectory()){
	    	arrayListCase=new ArrayList<String>();
	    	String[] lists = keyboarddirs.list();
	    	for(String var:lists){
	    		arrayListCase.add(var);
	    	}
	    }
	    return arrayListCase;
	}
}
